nums = [1, 0, -1, 0, -2, 2], target = 0
// -2  -1  0  0  1  2
var fourSum = function (nums, target) {
    if (nums.length < 4) return [];
    let result = []
    nums.sort((a, b) => a - b);
    for (let k = 0; k <= nums.length - 4; k++) {
        if (nums[k] > target && nums[k] > 0) break
        if (k > 0 && nums[k] === nums[k - 1]) continue
        for (let i = k + 1; i <= nums.length - 3; i++) {
            if (nums[k] + nums[i] > target && nums[k] > 0) break
            if (i > k + 1 && nums[i] === nums[i - 1]) continue
            let left = i + 1
            let right = nums.length - 1
            while (left < right) {
                let sum = nums[k] + nums[i] + nums[left] + nums[right]
                if (sum > target) {
                    right--
                } else if (sum < target) {
                    left++
                } else {
                    result.push([nums[k], nums[i], nums[left], nums[right]])
                    left++
                    right--
                    while (left < right && nums[left] === nums[left - 1]) left++
                    while (left < right && nums[right] === nums[right + 1]) right--
                }
            }
        }
    }
    return result
};